package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.kgd.news.entity.Results;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtils;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/8
 * @Time: 下午5:08
 */
public class ResultsController extends BaseController {

    @Override
    public void list() {
        String type = getPara("type");
        String score = getPara("score");
        String sql = "from results where 1 = 1";
        String querytype = "results";
        if (!StringUtils.isEmpty(type)) {
            sql += " and type = " + type;
            querytype += type;
        }
        if (!StringUtils.isEmpty(score)) {
            sql += " and score = " + score;
            querytype += score;
        }
        sql += " order by id desc";
        Page<Results> data = Results.dao.paginate(getPageIndex(querytype), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("results_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("did");
        Results res = Results.dao.findById(id);
        setAttr("res", res);
        render("results_edit.html");
    }

    @Override
    public void save() {
        Results sd = getModel(Results.class, "res");
        if(sd != null && sd.getId() != null){
            sd.update();
        }else{
            sd.save();
        }

        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from results where id in (" + ids + ")");

        list();
    }
}
